<?php
/**
 * Created by PhpStorm.
 * User: fuguang
 * Date: 2018/3/19
 * Time: 下午5:03
 */
use yii\helpers\Html;
//use yii\bootstrap\ActiveForm;
use anruence\editormd\EditorMd;
//use common\widgets\ueditor\Ueditor;
use common\widgets\ueditor\Ueditor;
use common\widgets\avatar\AvatarWidget;
use common\widgets\avatar\AvatarViewWidget;
use yii\widgets\ActiveForm;
use janisto\timepicker\TimePicker;
use yii\helpers\ArrayHelper;
use backend\models\NewsBanner;
?>
<div class="blog-create">
    <h1 class="text-center"><?php //Html::encode($this->title) ?></h1>
    <div class='blog-form'>
        <?php $form = ActiveForm::begin([
            'options' => ['enctype' => 'multipart/form-data', 'id' => "news-flash-post"],]) ?>

        <div>
            <div style="float: left;">
                <?= $form->field($model, 'newsurl')->textInput(['maxlength' => true,'style' => 'width:500px']) ?>
            </div>
            <div style="float: left;margin-top: 25px;">
                <?= Html::button("添加", ['class' => 'btn btn-default getbanner']) ?>
            </div>
            <div class="clearfix"></div>
            <?= $form->field($model, 'title')->textInput(['maxlength' => true,'readonly' => 'readonly']) ?>
            <?= $form->field($model, 'image')->hiddenInput(['maxlength' => true]) ?>
            <div style="width: 200px;min-height: 100px;border: 1px solid #ccc;margin-bottom: 20px;"><img id="viewbanner" src="" width="200px;"/></div>
            <?= $form->field($model, 'newsid')->hiddenInput()->label("") ?>
            <?= $form->field($model, 'position')->radioList(['0' => '否', '1' => '是'])->label('列表显示'); ?>
            <?= $form->field($model, 'status')->radioList(['2' => '草稿', '0' => '立即发布'])->label('发布方式'); ?>
            <div class='form-group field-blog-submit' style="margin-top: 20px;">
                <?php
                if( NewsBanner::bannerNum() >= 4 ){
                    echo Html::submitButton($model->isNewRecord ? '确认发布' : '确认修改', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'disabled' => 'disabled']);
                }else{
                    echo Html::submitButton($model->isNewRecord ? '确认发布' : '确认修改', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
                }
                ?>
            </div>
        </div>

        <?php ActiveForm::end(); ?>
    </div>
</div>
<script type="text/javascript">
    $(function(){
        $(".field-newsnom-ptime").hide();
        var _status = $("input[name='NewsNom[status]']:checked").val();
        if( _status == 3 ){
            $(".field-newsnom-ptime").show();
        }
        $("input[name='NewsNom[status]']").change(function () {
            if( $(this).val() == 3 ){
                $(".field-newsnom-ptime").show();
            }else{
                $(".field-newsnom-ptime").hide();
                $("#newsnom-ptime").val("");
            }
        });

        $(".getbanner").click(function () {
            var newsurl = $("#newsbanner-newsurl").val();
            $.post('/newsnom/ajax-get-news',{"newsurl": newsurl},function (result) {
                if( result.ret ){
                    var imageobj = result.data;
                    $("#newsbanner-title").val(imageobj.title);
                    $("#newsbanner-newsid").val(imageobj.id);
                    $("#newsbanner-image").val(imageobj.image);
                    $("#viewbanner").attr("src",imageobj.image);
                }
            },"json");
        });

        $('input[name="NewsBanner[position]"]').change(function () {
            $.get("/newsnom/has-list-banner",);
            var _val = $(this).val();
            var ret = true;
            if( _val*1 ){
                $.get("/newsnom/has-list-banner", function(result){
                    if( result*1 ){
                        alert("已存在");
                        $('input[name="NewsBanner[position]"]').removeAttr('checked');
                    }
                });
            }
            return ret;
        });
    });
</script>

